<template>
  <el-dialog
    title="修改"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
  <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="登录帐号:" >
        <el-input  v-model="dataForm.username" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="手机号:" >
        <el-input  v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="用户姓名:" >
        <el-input  v-model="dataForm.name" placeholder="用户姓名"></el-input>
      </el-form-item>
      <el-form-item label="社区名称:" >
        <el-input  v-model="dataForm.communityName" placeholder="社区名称"></el-input>
      </el-form-item>
      <el-form-item label="类型:" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="邀请码:" >
        <el-input  v-model="dataForm.invitationCode" placeholder="邀请码"></el-input>
      </el-form-item>
      <el-form-item label="积分总数:" >
        <el-input  v-model="dataForm.point" placeholder="积分总数"></el-input>
      </el-form-item>
      <el-form-item label="是否认证:" prop="authentication">
        <div v-if="dataForm.authentication == 0">是</div>
        <div v-if="dataForm.authentication == 1">否</div>
      </el-form-item>
      <el-form-item label="会员过期时间:" >
        <el-input  v-model="dataForm.memberEndTime" placeholder="会员过期时间"></el-input>
      </el-form-item>
      </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
export default {
  data() {
    return {
      dialogImageUrlss: '', //头像
      dialogVisibless: false,
      url:'',
      imageUrl: '',
      visible: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      imagesList:[],
      dataForm: {
        id: "",
        title: "",
        zj: "",
        dj: "",
        images: "",
        sl: "",
        headPhoto: "",
        status: "",
      },
      options: [
        {
          value: 1,
          label: "普通会员",
        },
        {
          value: 2,
          label: "超级会员",
        },
        {
          value: 3,
          label: "普通用户",
        },
      ],
      dataRule: {
        sl: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        headPhoto: [
          { required: true, message: "头像不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    inputChangezj(data) {
      var min = 1
      if (Number(this.dataForm.zj.replace(/[^\d]/g, '')) < min) {
        this.dataForm.zj = min
      } else {
        this.dataForm.zj = this.dataForm.zj.replace(/[^\d]/g, '')
      }
    },
    inputChangedj(data) {
      var min = 1
      if (Number(this.dataForm.dj.replace(/[^\d]/g, '')) < min) {
        this.dataForm.dj = min
      } else {
        this.dataForm.dj = this.dataForm.dj.replace(/[^\d]/g, '')
      }
    },
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.dataForm.headPhoto = res.url
      },
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 5;
       
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 5MB!');
        }
        return isLt2M;
      },
      handleAvatarSuccesses(res, file) {
        this.imagesList.push(res.url)
      },
      handleRemovess(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreviewss(file) {
        this.dialogImageUrlss = file.url;
        this.dialogVisibless = true;
      },
    init(id) {
      this.url = this.$http.adornUrl(`app/oss/upload?token=${this.$cookie.get('token')}`)
      this.dataForm.id = id || "";
      this.visible = true;
      if(this.dataForm.id) {
        this.$http({
            url: this.$http.adornUrl(
              `yshbms/user/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm= data.userEntity;
            }
          });
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`yh/user/updateTx`),
              method: "post",
              params: this.$http.adornParams({
              userId: this.dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          } else {
          }
        }
      });
    },
  },
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>